﻿using NDK.Motion.MachineManager;
using HZH_Controls.Forms;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;

namespace NDK.Motion.Frm
{
    public partial class Dlg_SysIOConfig : Form
    {
        public Dlg_SysIOConfig()
        {
            InitializeComponent();
            btn_Confirm.Visible = true;

            List<string> diNames = new List<string>();
            List<string> doNames = new List<string>();
            foreach (var item in MachineDataManager.Instance.DOList)
            {
                doNames.Add(item.Name);
            }
            foreach (var item in MachineDataManager.Instance.DIList)
            {
                diNames.Add(item.Name);
            }


            foreach (var diItem in gb_SysDI.Controls)
            {
                if (diItem is ComboBox)
                {
                    ((ComboBox)diItem).Items.Clear();
                    ((ComboBox)diItem).Items.AddRange(diNames.ToArray());
                }
            }

            foreach (var doItem in gb_SysDO.Controls)
            {
                if (doItem is ComboBox)
                {
                    ((ComboBox)doItem).Items.Clear();
                    ((ComboBox)doItem).Items.AddRange(doNames.ToArray());
                }
            }
        }

        private void btn_Confirm_BtnClick(object sender, EventArgs e)
        {
            string name = string.Empty;
            //启动按钮
            name = cb_Start.Text.Trim();
            if (name != "")
            {
                MachineDataManager.Instance.SYS_DI_Start = MachineDataManager.Instance.DIList.SingleOrDefault(d => d.Name == name);
            }
            //暂停按钮
            name = cb_Pause.Text.Trim();
            if (name != "")
            {
                MachineDataManager.Instance.SYS_DI_Pause = MachineDataManager.Instance.DIList.SingleOrDefault(d => d.Name == name);
            }
            //复位按钮
            name = cb_Homing.Text.Trim();
            if (name != "")
            {
                MachineDataManager.Instance.SYS_DI_Reset = MachineDataManager.Instance.DIList.SingleOrDefault(d => d.Name == name);
            }
            //急停按钮
            name = cb_Emg.Text.Trim();
            if (name != "")
            {
                MachineDataManager.Instance.SYS_DI_Emg = MachineDataManager.Instance.DIList.SingleOrDefault(d => d.Name == name);
            }
            //安全光幕
            name = cb_SaftyScreen.Text.Trim();
            if (name != "")
            {
                MachineDataManager.Instance.SYS_DI_SafetyScreen = MachineDataManager.Instance.DIList.SingleOrDefault(d => d.Name == name);
            }
            //安全门
            name = cb_SaftyDoor.Text.Trim();
            if (name != "")
            {
                MachineDataManager.Instance.SYS_DI_SafetyDoor = MachineDataManager.Instance.DIList.SingleOrDefault(d => d.Name == name);
            }
            //红灯
            name = cb_RedLamp.Text.Trim();
            if (name != "")
            {
                MachineDataManager.Instance.SYS_DO_RedLamp = MachineDataManager.Instance.DOList.SingleOrDefault(d => d.Name == name);
            }
            //绿灯
            name = cb_GreenLamp.Text.Trim();
            if (name != "")
            {
                MachineDataManager.Instance.SYS_DO_GreenLamp = MachineDataManager.Instance.DOList.SingleOrDefault(d => d.Name == name);
            }
            //黄灯
            name = cb_YellowLamp.Text.Trim();
            if (name != "")
            {
                MachineDataManager.Instance.SYS_DO_YellowLamp = MachineDataManager.Instance.DOList.SingleOrDefault(d => d.Name == name);
            }
            //蜂鸣器
            name = cb_Buzzer.Text.Trim();
            if (name != "")
            {
                MachineDataManager.Instance.SYS_DO_Buzzer = MachineDataManager.Instance.DOList.SingleOrDefault(d => d.Name == name);
            }
            //手自动
            name = cb_AutoManuel.Text.Trim();
            if (name != "")
            {
                MachineDataManager.Instance.SYS_DO_AutoManuel = MachineDataManager.Instance.DOList.SingleOrDefault(d => d.Name == name);
            }

            DialogResult = DialogResult.OK;
            FrmTips.ShowTipsSuccess(this, "保存成功！");
        }

        private void Dlg_SysIOConfig_Load(object sender, EventArgs e)
        {
            //启动按钮
            if (MachineDataManager.Instance.SYS_DI_Start != null)
            {
                cb_Start.Text = MachineDataManager.Instance.SYS_DI_Start.Name;
            }
            //暂停按钮
            if (MachineDataManager.Instance.SYS_DI_Pause != null)
            {
                cb_Pause.Text = MachineDataManager.Instance.SYS_DI_Pause.Name;
            }
            //复位按钮
            if (MachineDataManager.Instance.SYS_DI_Reset != null)
            {
                cb_Homing.Text = MachineDataManager.Instance.SYS_DI_Reset.Name;
            }
            //急停按钮
            if (MachineDataManager.Instance.SYS_DI_Emg != null)
            {
                cb_Emg.Text = MachineDataManager.Instance.SYS_DI_Emg.Name;
            }
            //安全光幕
            if (MachineDataManager.Instance.SYS_DI_SafetyScreen != null)
            {
                cb_SaftyScreen.Text = MachineDataManager.Instance.SYS_DI_SafetyScreen.Name;
            }
            //安全门
            if (MachineDataManager.Instance.SYS_DI_SafetyDoor != null)
            {
                cb_SaftyDoor.Text = MachineDataManager.Instance.SYS_DI_SafetyDoor.Name;
            }
            //红灯
            if (MachineDataManager.Instance.SYS_DO_RedLamp != null)
            {
                cb_RedLamp.Text = MachineDataManager.Instance.SYS_DO_RedLamp.Name;
            }
            //绿灯
            if (MachineDataManager.Instance.SYS_DO_GreenLamp != null)
            {
                cb_GreenLamp.Text = MachineDataManager.Instance.SYS_DO_GreenLamp.Name;
            }
            //黄灯
            if (MachineDataManager.Instance.SYS_DO_YellowLamp != null)
            {
                cb_YellowLamp.Text = MachineDataManager.Instance.SYS_DO_YellowLamp.Name;
            }
            //蜂鸣器
            if (MachineDataManager.Instance.SYS_DO_Buzzer != null)
            {
                cb_Buzzer.Text = MachineDataManager.Instance.SYS_DO_Buzzer.Name;
            }
            //手自动
            if (MachineDataManager.Instance.SYS_DO_AutoManuel != null)
            {
                cb_AutoManuel.Text = MachineDataManager.Instance.SYS_DO_AutoManuel.Name;
            }
        }

    }
}
